function _toConsumableArray(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
    return Array.from(t);
}

var g, pages, app = getApp(), page = app.page, request = app.util.request,_tools = require("../../utils/tools"),toast = app.toast;

function getList(type_id = '') {
  if(g.data.system && g.data.system.is_force_login){
    return void (0, _tools.verifyLogin)({
      success: function() {
        var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : -1, a = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 1;
        console.log(pages)
        if(!type_id && g.data.TabCur >= 0){
          type_id = g.data.news_type[ g.data.TabCur].id
        }
        let userInfo = wx.getStorageSync("userInfo")
        let user_id = g.data.userInfo.id
        let url = "NewsList"
        if(g.data.TabCur >= 0){
          let category = g.data.news_type[g.data.TabCur]
          if(category.is_active == 1){
            url = "ActiveList"
            request({
              url: "ActiveAudit",
              data: {
                type_id,
                user_id: g.data.userInfo.id
              },
              success: function(t) {
                g.setData({
                  is_audit: t.is_audit
                })
              }
            })
          } else if(type_id == 28){
            url = "MettingList"
          } else if(type_id == 30){
            url = "LawPartyList"
          } else if(type_id == 47){
            url = "NvshangList"
          } else if(type_id == 43){
            url = "InformationCenterList"
            if(g.data.messageNavIndex == 0){
              user_id = ''
            }
          }
        }

        
        request({
            url,
            data: {
                type_id,
                page: pages,
                number: app.config.news_number,
                title: g.data.keyword,
                user_id
            },
            showLoading: !1,
            success: function(t) {
                wx.stopPullDownRefresh();
                var e = g.data.news_list;
                e && 1 != pages && (t = [].concat(_toConsumableArray(e), _toConsumableArray(t))), g.setData({
                    news_list: t,
                }), t.length && app.config.news_number >= t.length && g.setData({
                    isLoad: !0
                });
            }
        });
        
        if(userInfo){
          let type_index = g.data.news_type.findIndex((item)=>{
            return item.id == type_id
          })
          request({
            url: "AddUsersLog",
            data: {
              user_id: userInfo.id,
              url: '/u_pages/news/index',
              title: "发现" + (type_index !== -1 ? ( "/" + g.data.news_type[type_index].title) : '')
            },
            success: function(a) {
            }
          })
        }
      }
    });
  }
    
}

page({
    data: {
        TabCur: -1,
				isLoad: !1,
        keyword: '',
        isShow: false,
        username: '',
        phone: '',
        private_id: '',
        formType: 1,
        messageNavIndex: 0,
        is_audit: 0
    },
    watch:{
      userInfo:function(newValue){
        if(newValue){
          getList()
        }
      }
    },
    onLoad: function(t) {
        getApp().setWatcher(this.data, this.watch);
        pages = 1, (g = this).options && g.options.id ?  wx.setNavigationBarTitle({
            title: g.options.title || "新闻"
        }) : ''
        request({
            url: "NewsType",
            showLoading: !1,
            success: function(t) {
                // {
                //     id: -1,
                //     title: "全部"
                // } 
                g.setData({
                    news_type: [ ].concat(_toConsumableArray(t))
                }) 
                if(g.options.id){
                    let TabCur = g.data.news_type.findIndex((item)=>{return item.id == g.options.id})
                    g.setData({
                        TabCur
                    })
                }
                getList(g.options.id)
            }
        });
		},
		keywordSearch: function(){
				getList()
		},
		input: function(a) {
			let keyword = a.detail.value;
			g.setData({
				keyword
			})	
		},
    onPullDownRefresh: function() {
        g.setData({
            TabCur: 0,
            isLoad: !1
        }), g.onLoad();
    },
    onReachBottom: function() {
        !g.data.isLoad && pages++ && getList(g.data.TabCur !== -1 ? g.data.news_type[g.data.TabCur].id : '', pages);
    },
    tabSelect: function(t) {
        t = t.currentTarget.dataset.item, g.setData({
            TabCur: t[0],
            isLoad: !1
        }), getList(t[pages = 1]);
    },
    onShareAppMessage: function(t) {
        return {
            title: g.data.system.xcx_name,
            path: g.route + "?pid=" + app.userInfo.id
        };
    },
    onShareTimeline: function() {
        return {
            title: g.data.system.xcx_name,
            query: "pid=" + app.userInfo.id
        };
    },
    getUsername(e) {
      let username = e.detail.value;
      g.setData({
        username
      })
    },
    getPhone(e) {
      let phone = e.detail.value;
      g.setData({
        phone
      })
    },
    isShow: function (t) {
      g.setData({
        isShow: false
      })
    },
    submit: function(){
      if(!g.data.username){
        toast("请输入您的姓名")
        return false
      }
      if(!g.data.phone){
        toast("请输入您的手机号")
        return false
      }
      let url = 'ApplyActive'
      let type = g.data.news_type[g.data.TabCur].id
      request({
        url,
        data: {
          type_id:  type,
          user_id: app.userInfo.id,
          tel: g.data.phone,
          name: g.data.username,
        },
        success: function(t) {
          g.setData({
            formType: 2,
            is_audit: 2
          })
        }
      })
    },
    join: function(e){
      let private_id = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      if(g.data.is_audit == 0){
        g.setData({
          formType: 3,
          isShow: true,
        })
        return false
      } else if(g.data.is_audit == 2){
        g.setData({
          formType: 2,
          isShow: true,
        })
        return false
      }  
      request({
        url: "PartActive",
        data: {
          private_id,
          user_id: app.userInfo.id
        },
        success: function(t) {
          g.data.news_list[index].is_join = 1
          g.setData({
            formType: 4,
            isShow: true,
            news_list: g.data.news_list
          })
        }
      })
    },
    apply: function(e){
      if(g.data.is_audit != 0){
        if(g.data.is_audit == 2){
          g.setData({
            formType: 2,
            isShow: true,
          })
        }
        return false
      }
      g.setData({
        formType: 1,
        isShow: true,
        username: '',
        phone: ''
      })
    },
    goDetail: function(e){
      let type = g.data.news_type[g.data.TabCur].id
      let id = e.currentTarget.dataset.item
      wx.navigateTo({
        url: '/u_pages/news/metting?id='+id + "&type=" + type+"&title="+g.data.news_type[g.data.TabCur].title,
      })
    },
    messageNavChange: function(e){
      let index = e.currentTarget.dataset.index
      if(index == 1){
        wx.navigateTo({
          url: '/u_pages/message/add/index',
        })
      } else {
        pages = 1
        g.setData({
          messageNavIndex: index,
          isLoad: !1
        },() => {
          getList();
        })
        
      }
    }
});